package ee.az.tetris.worker;

import ee.az.tetris.Glass;
import ee.az.tetris.event.Event;
import java.awt.Color;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class LeftShiftWorker extends Worker {
	
	public LeftShiftWorker(Glass glass) {
		super(glass);
	}

	@Override
	public void work(Event event) {
		boolean canShift = true;
		for (int col = 0; col < getGlass().getColumnCount(); col++) {
			for (int row = getGlass().getRowCount()-1; row >= 0 ; row--) {
				Color value = getGlass().getValue(col, row);
				if (Glass.FLOATING.equals(value)) {
					if (!isAvaliable(col-1, row, getGlass())) {
						canShift = false;
					}
				}
			}
		}
		if (canShift) { // then fall
			for (int col = 0; col < getGlass().getColumnCount(); col++) {
				for (int row = getGlass().getRowCount()-1; row >= 0 ; row--) {
					Color value = getGlass().getValue(col, row);
					if (Glass.FLOATING.equals(value)) {
						getGlass().setValue(value, col-1, row);
						getGlass().setValue(Glass.EMPTY, col, row);
					}
				}
			}
		}
	}
}
